//
//  AddPetViewController.m
//  TaTaBang
//
//  Created by 李禹 on 15/8/22.
//  Copyright © 2015年 hailong. All rights reserved.
//

#import "AddPetViewController.h"
#import "NJSettingSwitchItem.h"
#import "NJSettingArrowItem.h"
#import "NJSettingGroup.h"
#import "NJProductCell.h"
#import "NJSettingTextFieldItem.h"
#import "NJSettingButtonItem.h"
#import <AssetsLibrary/AssetsLibrary.h>

@interface AddPetViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate>

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation AddPetViewController

#pragma mark - 懒加载
- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        // 第一组数据
        NJSettingItem *item00 = [[NJSettingTextFieldItem alloc] initWithTitle:@"犬主" placeholder:@"安在宣"];
        
        NJSettingItem *item01 = [[NJSettingTextFieldItem alloc] initWithTitle:@"手机号" placeholder:@"15000000000" keyboardType:UIKeyboardTypeNumberPad];
        
        NJSettingGroup *group1 = [[NJSettingGroup alloc] init];
        group1.items = @[item00 ,item01];
        
        // 第2组数据
        NJSettingItem *item10 = [[NJSettingButtonItem alloc] initWithTitle:@"爱宠照片" iconName:@"pet"];
        __weak typeof(self) this = self;
        item10.option = ^() {
            UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:nil delegate:this cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从手机相册选择", nil];
            menu.actionSheetStyle = UIBarStyleBlackTranslucent;
            [menu showInView:this.view];
        };
        NJSettingGroup *group2 = [[NJSettingGroup alloc] init];
        group2.items = @[item10];
        
        // 第3组数据
        NJSettingItem *item20 = [[NJSettingTextFieldItem alloc] initWithTitle:@"爱宠名" placeholder:@"小豆豆"];
        
        NJSettingItem *item21 = [[NJSettingTextFieldItem alloc] initWithTitle:@"犬种" placeholder:@"泰迪"];
        NJSettingItem *item22 = [[NJSettingSwitchItem alloc] initWithTitle:@"性别"];
        NJSettingItem *item23 = [[NJSettingTextFieldItem alloc] initWithTitle:@"犬龄" placeholder:@"1岁" keyboardType:UIKeyboardTypeNumberPad];
        NJSettingItem *item24 = [[NJSettingTextFieldItem alloc] initWithTitle:@"毛色" placeholder:@"棕色"];
        NJSettingItem *item25 = [[NJSettingTextFieldItem alloc] initWithTitle:@"肩高" placeholder:@"82cm" keyboardType:UIKeyboardTypeNumbersAndPunctuation];
        NJSettingItem *item26 = [[NJSettingTextFieldItem alloc] initWithTitle:@"体重" placeholder:@"4kg" keyboardType:UIKeyboardTypeNumbersAndPunctuation];

        NJSettingGroup *group3 = [[NJSettingGroup alloc] init];
        group3.items = @[item20, item21, item22, item23, item24, item25, item26];
        
        _dataArray = [NSMutableArray array];
        [_dataArray addObject:group1];
        [_dataArray addObject:group2];
        [_dataArray addObject:group3];
    }
    return _dataArray;
}



- (void)viewDidLoad {
    [super viewDidLoad];

    [self setUI];

    self.title = @"添加宠物";
}

- (void)setUI {
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 90)];
    UIButton *commitButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [commitButton setBackgroundImage:[UIImage resizableImageWithName:@"btn_background"] forState:UIControlStateNormal];
    [commitButton setBackgroundImage:[UIImage resizableImageWithName:@"btn_background"] forState:UIControlStateHighlighted];
    [commitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [commitButton setTitle:@"提交" forState:UIControlStateNormal];
    [commitButton addTarget:self action:@selector(submit:) forControlEvents:UIControlEventTouchUpInside];
    commitButton.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    commitButton.frame = CGRectMake(22, 20, SCREEN_WIDTH - 44, 50);
    [footerView addSubview:commitButton];
    self.tableView.tableFooterView = footerView;
}

- (id)init {
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (id)initWithStyle:(UITableViewStyle)style {
    return [super initWithStyle:UITableViewStyleGrouped];
}

#pragma mark - UIButton Action Method
- (void)submit:(UIButton *)btn {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // 先取出对应组的小数组
    NJSettingGroup *g = self.dataArray[section];
    // 返回小数组的长度
    return g.items.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 1.创建cell
    NJProductCell *cell = [NJProductCell cellWithTableView:tableView];
    
    // 先取出对应组的组模型
    NJSettingGroup *g = self.dataArray[indexPath.section];
    //  从组模型中取出对应行的模型
    NJSettingItem *item = g.items[indexPath.row];
    cell.item = item;
    
    // 3.返回cell
    return cell;
}

- (CGFloat)tableView:(nonnull UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        return 80;
    }else {
        return 44;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 立即取消选中
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    // 先取出对应组的组模型
    NJSettingGroup *g = self.dataArray[indexPath.section];
    //  从组模型中取出对应行的模型
    NJSettingItem *item = g.items[indexPath.row];
    // 判断block中是否保存了代码
    if (item.option != nil) {
        // 如果保存,就执行block中保存的代码
        item.option();
    }else if ([item isKindOfClass:[NJSettingArrowItem class]]) {
        
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    // 先取出对应组的组模型
    NJSettingGroup *g = self.dataArray[section];
    return g.headerTitle;
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    // 先取出对应组的组模型
    NJSettingGroup *g = self.dataArray[section];
    return g.footerTitle;
}

#pragma mark - HiddenKeyboard
- (void)scrollViewDidScroll:(nonnull UIScrollView *)scrollView {
    [self.view endEditing:YES];
}


#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(nonnull UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            imagePicker.delegate = self;
            [self presentViewController:imagePicker animated:YES completion:nil];
        }else {
            UIAlertView *alert =[[UIAlertView alloc] initWithTitle:nil message:@"相机不能用" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
            [alert show];
        }
    }else if(buttonIndex == 1){
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) return;
        
        UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
        ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        ipc.delegate = self;
        [self presentViewController:ipc animated:YES completion:nil];
    }

}

#pragma mark UIImagePickerController Method
//完成拍照
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    UIImage *image = info[UIImagePickerControllerOriginalImage];
//    NJProductCell *cell = [
}

//用户取消拍照
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}



@end
